$(function () {
	$("#jqGrid").jqGrid({
		url: baseURL + 'school/school/list',
		datatype: "json",
		postData:{province:"山东省"},
		colModel: [
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true , hidden:true},
			{ label: '学校代码', name: 'code', index: 'code', width: 80 },
			{ label: '学校名称', name: 'name', index: 'name', width: 80 },
			{ label: '学校性质', name: 'nature', index: 'nature', width: 80},
			{ label: '所在地区', name: 'area', index: 'area', width: 80 },
			{ label: '所在省份', name: 'province', index: 'province', width: 80 },
			{ label: '院校分类（普通,医学，艺术,军警体）', name: 'category', index: 'professionalCategory', width: 80},
			{ label: '层次（1本科，2专科，3本专混合）', name: 'level', index: 'level', width: 80},
			{ label: '包含专业形式（1普通，2校企合作，3中外合作, 4 3+2连读）', name: 'learntype', index: 'learnType', width: 80 },
			{ label: '推荐理由', name: 'recommendedreasons', index: 'recommendedReasons', width: 80 }
		],
		viewrecords: true,
		height: 385,
		rowNum: 10,
		rowList : [10,30,50],
		rownumbers: true,
		rownumWidth: 25,
		autowidth:true,
		multiselect: true,
		pager: "#jqGridPager",
		jsonReader : {
			root: "page.list",
			page: "page.currPage",
			total: "page.totalPage",
			records: "page.totalCount"
		},
		prmNames : {
			page:"page",
			rows:"limit",
			order: "order"
		},
		gridComplete:function(){
			//隐藏grid底部滚动条
			$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
		}
	});
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		school: {},
		learntype:[],
		professionalcategory:[],
		area:[]
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.school = {
				nature:"公办",
				area:'济南',
				enrolmentmethod:"夏考",
				professionalcategory:"普通",
				level:"本科",
				learntype:"",
				area:"",
				province:"山东省"

			};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
			vm.title = "修改";

			vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var learntype=""
			var professionalcategory=""
			var areaA=""
			for(var i=0;i<vm.learntype.length;i++){
				learntype=learntype+vm.learntype[i]+","
			}
			for(var i=0;i<vm.professionalcategory.length;i++){
				professionalcategory=professionalcategory+vm.professionalcategory[i]+","
			}
			for(var i=0;i<vm.area.length;i++){
				areaA=areaA+vm.area[i]+","
			}
			vm.school.learntype=learntype
			vm.school.category=professionalcategory
			vm.school.area=areaA
			var url = vm.school.id == null ? "school/school/save" : "school/school/update";
			if(vm.school.cities == "" || vm.school.cities == null || vm.school.cities == undefined){
				alert("城市不能为空")
			}else{
				vm.school.region = "华东"
				vm.school.province = "山东"
				$.ajax({
					type: "POST",
					url: baseURL + url,
					contentType: "application/json",
					data: JSON.stringify(vm.school),
					success: function(r){
						if(r.code === 0){
							alert('操作成功', function(index){
								vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			}
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}

			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
					url: baseURL + "school/school/delete",
					contentType: "application/json",
					data: JSON.stringify(ids),
					success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "school/school/info/"+id, function(r){
				vm.school = r.school;
				vm.learntype = r.school.learntype.split(",");
				vm.professionalcategory = r.school.category.split(",");
				vm.area = r.school.area.split(",");

			});
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				page:page
			}).trigger("reloadGrid");
		},
		setLastThreeYear:function () {
			var year=["2018","2017","2016"]
			confirm('确定要设置近三年？', function(){
				$.ajax({
					type: "POST",
					url: baseURL + "school/school/setLastThreeYear",
					contentType: "application/json",
					data: JSON.stringify(year),
					success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		}
	}
});